//
//  BZAlert.m
//  BiZhi
//
//  Created by ryou on 2021/7/16.
//

#define kBZkSpace 20.0

#define kBZkItemWidth (kBZScreenW - kBZkSpace * 6)/3
#define kBZkBGWidth kBZkItemWidth * 3 + kBZkSpace * 4

#define kBZkBGHeight kBZScreenH - 200.0

#import "BZAlert.h"
@interface BZAlert ()

@property(nonatomic,copy)void(^didSelectTag)(void);

@end
@implementation BZAlert

static BZAlert *_instance=nil;

+(BZAlert *)sharedInstance{
    
    if (_instance==nil)
    {
        _instance=[[super alloc]init];
    }
    return _instance;
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self==[super initWithFrame:frame]) {
        UIView *window = [UIApplication sharedApplication].keyWindow;
        [window addSubview:self];
        self.frame=CGRectMake(0, 0, kBZScreenW, kBZScreenH);
        self.userInteractionEnabled = YES;
        self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    
    }

    return self;

}

-(void)bzshowAlertWithWithTag:(void (^)(void))didSelectTag{
    _didSelectTag = didSelectTag;
    
    [UIView animateWithDuration:0.1 animations:^{

        self.backgroundColor =  [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
   
    } completion:^(BOOL finished) {
 
        CGFloat kBZwwww1 = kBZScreenW;
        CGFloat kBZwwww2 = kBZkBGWidth;
        CGFloat kBZwwww3 = kBZwwww1-kBZwwww2;
        
        CGFloat kBZhhh1 = kBZScreenH;
        CGFloat kBZhhh2 = kBZkBGHeight;
        CGFloat kBZhhh3 = kBZhhh1-kBZhhh2;
        UIView *bg=[[UIView alloc]initWithFrame:CGRectMake(kBZwwww3/2,kBZhhh3/2, kBZkBGWidth , kBZkBGHeight)];
        bg.backgroundColor=[UIColor colorWithRed:0.926 green:0.949 blue:0.976 alpha:1.0];
        bg.layer.cornerRadius = 10;
        [self addSubview:bg];
        
        UILabel *kBZjiesuo=[[UILabel alloc]initWithFrame:CGRectMake(0, 10, bg.frame.size.width, 50)];
        kBZjiesuo.text = @"解锁此功能";
        kBZjiesuo.textAlignment=NSTextAlignmentCenter;
        kBZjiesuo.font = [UIFont boldSystemFontOfSize:25];
        kBZjiesuo.textColor=[UIColor colorWithRed:0.896 green:0.409 blue:0.576 alpha:1.0];
        [bg addSubview:kBZjiesuo];
        
    
        
        UIButton *kBZsavebtn=[[UIButton alloc]initWithFrame:CGRectMake(bg.frame.size.width / 2 - 70, bg.frame.size.height - 70, 140, 50)];
//        [kBZsavebtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [kBZsavebtn setBackgroundColor:[UIColor colorWithRed:0.397 green:0.859 blue:0.471 alpha:1.0]];
        [bg addSubview:kBZsavebtn];
        kBZsavebtn.layer.cornerRadius = 25;
        kBZsavebtn.clipsToBounds=YES;
        [kBZsavebtn setTitle:@"永久解锁" forState:UIControlStateNormal];
        [kBZsavebtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [kBZsavebtn addTarget:self action:@selector(kBZsavebtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self shakeToShow:kBZsavebtn];
        
        
        UILabel *kBZjiesuotips=[[UILabel alloc]initWithFrame:CGRectMake(0,kBZjiesuo.frame.size.height+ kBZjiesuo.frame.origin.y +  10, bg.frame.size.width, 30)];
        kBZjiesuotips.text = @"完整观看此视频（约30s）";
        kBZjiesuotips.textAlignment=NSTextAlignmentCenter;
        kBZjiesuotips.font = [UIFont boldSystemFontOfSize:15];
        kBZjiesuotips.textColor=[UIColor colorWithRed:0.616 green:0.642 blue:0.694 alpha:1.0];
        [bg addSubview:kBZjiesuotips];
        
        
        UIImageView *kBZtipsimage=[[UIImageView alloc]initWithFrame:CGRectMake(20, kBZjiesuotips.frame.size.height+kBZjiesuotips.frame.origin.y+ 10, bg.frame.size.width - 40, bg.frame.size.height - 50 - 50- 30  - 10-10-20-10 - 10)];
        kBZtipsimage.image=[UIImage imageNamed:@"kkkkk.jpeg"];
        kBZtipsimage.contentMode=UIViewContentModeScaleAspectFit;
        [bg addSubview:kBZtipsimage];
    }];

}
- (void) shakeToShow:(UIView *)aView
{
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.6;// 动画时间
    NSMutableArray *values = [NSMutableArray array];
    // 前两个是控制view的大小的；
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    animation.repeatCount=MAXFLOAT;
    [aView.layer addAnimation:animation forKey:nil];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self hidden];
}

-(void)hidden{
  
    [UIView animateWithDuration:0.1 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
                
        for (UIView *view in self.subviews) {
            [view removeFromSuperview];
        }
        [self removeFromSuperview];
        self->_didSelectTag = nil;
        _instance = nil;
    }];
}
-(void)kBZsavebtnClick{

    if(self.didSelectTag ){
        self.didSelectTag();
    }
    [self hidden];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


@end
